package com.clayecloud.module_main.viewModels;

import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.clayecloud.lib_common.apis.HouseApi;
import com.clayecloud.lib_common.apis.LoginApi;
import com.clayecloud.lib_common.utils.GsonUtil;
import com.clayecloud.lib_common.utils.HttpUtil;
import com.clayecloud.lib_common.utils.LoginUtil;
import com.clayecloud.lib_common.utils.MessageUtil;
import com.clayecloud.module_main.entity.News;
import com.clayecloud.module_main.entity.TitleText;
import java.util.ArrayList;
import java.util.List;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class FragmentHomeViewModel extends ViewModel {

    //登录请求的api
    private LoginApi loginApi;
    //房屋请求的api
    private HouseApi houseApi;

    private MutableLiveData<Boolean> bool_relogin;

    //推荐数据
    private MutableLiveData<List<News>> newsList;

    //顶部导航栏数据
    private MutableLiveData<TitleText> titleTextMutableLiveData;

    //标题改变对外提供变化监听
    private MutableLiveData<Boolean>  textwIsChange;

    private Integer leftHeaderButton;

    private boolean islogin;

    /*-------------------------------getter-setter---------------------------------*/

    public MutableLiveData<List<News>> getNewsList() {
        return newsList;
    }

    public void setNewsList(MutableLiveData<List<News>> newsList) {
        this.newsList = newsList;
    }

    public MutableLiveData<TitleText> getTitleTextMutableLiveData() {
        return titleTextMutableLiveData;
    }

    public void setTitleTextMutableLiveData(MutableLiveData<TitleText> titleTextMutableLiveData) {
        this.titleTextMutableLiveData = titleTextMutableLiveData;
    }

    public MutableLiveData<Boolean> getBool_relogin() {
        return bool_relogin;
    }

    public void setBool_relogin(MutableLiveData<Boolean> bool_relogin) {
        this.bool_relogin = bool_relogin;
    }

    public MutableLiveData<Boolean> getTextwIsChange() {
        return textwIsChange;
    }

    public void setTextwIsChange(MutableLiveData<Boolean> textwIsChange) {
        this.textwIsChange = textwIsChange;
    }

    public Integer getLeftHeaderButton() {
        return leftHeaderButton;
    }

    public void setLeftHeaderButton(Integer leftHeaderButton) {
        this.leftHeaderButton = leftHeaderButton;
    }

    public boolean getIslogin() {
        return islogin;
    }

    public void setIslogin(boolean islogin) {
        this.islogin = islogin;
    }

    /*-------------------------------getter-setter---------------------------------*/

    /**
     * 进行初始化数据
     */
    public void loadData () {
        //初始化推荐内容数据
        List<News> newsList = new ArrayList<>();
        newsList.add(new News("夜读 | 不要辜负主动联系你的人","所有不经意的主动背后，都藏着深深...","https://gw.alicdn.com/imgextra/i3/75/O1CN011kYrp81CQOJHwJ8uu_!!75-0-lubanu.jpg"));
        newsList.add(new News("夜读 | 不要辜负主动联系你的人","所有不经意的主动背后，都藏着深深...","https://gw.alicdn.com/imgextra/i3/75/O1CN011kYrp81CQOJHwJ8uu_!!75-0-lubanu.jpg"));
        newsList.add(new News("夜读 | 不要辜负主动联系你的人","所有不经意的主动背后，都藏着深深...","https://gw.alicdn.com/imgextra/i3/75/O1CN011kYrp81CQOJHwJ8uu_!!75-0-lubanu.jpg"));
        newsList.add(new News("夜读 | 不要辜负主动联系你的人","所有不经意的主动背后，都藏着深深...","https://gw.alicdn.com/imgextra/i3/75/O1CN011kYrp81CQOJHwJ8uu_!!75-0-lubanu.jpg"));
        newsList.add(new News("夜读 | 不要辜负主动联系你的人","所有不经意的主动背后，都藏着深深...","https://gw.alicdn.com/imgextra/i3/75/O1CN011kYrp81CQOJHwJ8uu_!!75-0-lubanu.jpg"));
        newsList.add(new News("夜读 | 不要辜负主动联系你的人","所有不经意的主动背后，都藏着深深...","https://gw.alicdn.com/imgextra/i3/75/O1CN011kYrp81CQOJHwJ8uu_!!75-0-lubanu.jpg"));
        newsList.add(new News("夜读 | 不要辜负主动联系你的人","所有不经意的主动背后，都藏着深深...","https://gw.alicdn.com/imgextra/i3/75/O1CN011kYrp81CQOJHwJ8uu_!!75-0-lubanu.jpg"));
        MutableLiveData<List<News>> newsListMutableLiveData = new MutableLiveData<>();
        newsListMutableLiveData.setValue(newsList);
        setNewsList(newsListMutableLiveData);
        //初始化头部导航栏数据
        MutableLiveData<TitleText> titleText = new MutableLiveData<>();
        titleText.setValue(new TitleText("您好，请登录 ","登录后开启智慧生活"));
        setTitleTextMutableLiveData(titleText);
        //初始化api接口
        loginApi = HttpUtil.getInstance().create(LoginApi.class);
        houseApi = HttpUtil.getInstance().create(HouseApi.class);
        //是否重新登录
        MutableLiveData<Boolean> bool_relogin = new MutableLiveData<>();
        bool_relogin.setValue(false);
        setBool_relogin(bool_relogin);
        //通知头部text变化
        MutableLiveData<Boolean> textwIsChange = new MutableLiveData<>();
        textwIsChange.setValue(false);
        setTextwIsChange(textwIsChange);
        //头部导航栏的左边初始状态
        leftHeaderButton = 0;
        //设置登录状态
        setIslogin(false);
    }

    /**
     * 认证登录,进行登录本地数据校验/更新一次数据
     * @param userID
     */
    public void authenticationLoginState(int userID){
        loginApi.getUserBaseInfo(LoginUtil.getUserToken(userID)).subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<MessageUtil>() {
                    private Disposable disposable;
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        disposable = d;
                    }

                    @Override
                    public void onNext(@NonNull MessageUtil messageUtil) {
                        if(messageUtil.getResult()){
                            //更新本地数据
                            LoginUtil.updateLocalUserData();
                            //进行数据的序列化
                            com.clayecloud.lib_common.apis.entity.login.ClientUser clientUser = GsonUtil.getInstance().fromJson(GsonUtil.getInstance().toJson(messageUtil.getObj()), com.clayecloud.lib_common.apis.entity.login.ClientUser.class);
                            //取出userid查看是否绑定房屋
                            checkUserBindHouse(clientUser.getUserID());
                            bool_relogin.setValue(false);
                            return;
                        }
                        bool_relogin.setValue(true);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.e("FragmentHomeViewModel => verifyToken => onError",e.getMessage());
                        LoginUtil.userLoginOut();
                        disposable.dispose();
                    }

                    @Override
                    public void onComplete() {
                        disposable.dispose();
                    }
                });
    }

    public void checkUserBindHouse(int userID){
        houseApi.checkUserBindHouse(userID).subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<MessageUtil>() {
                    private Disposable disposable;
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        disposable = d;
                    }

                    @Override
                    public void onNext(@NonNull MessageUtil messageUtil) {
                        if(messageUtil.getResult()){
                            if((boolean)messageUtil.getObj()){
                                changeHouse(2);
                            }else{
                                changeHouse(1);
                            }
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.e("FragmentHomeViewModel => checkUserBindHouse => onError",e.getMessage());
                        disposable.dispose();
                    }

                    @Override
                    public void onComplete() {
                        disposable.dispose();
                    }
                });
    }


    /**
     * 绑定房屋
     */
    public void changeHouse(Integer type){
        if(type == 2){
            MutableLiveData<TitleText> titleText = getTitleTextMutableLiveData();
            titleText.getValue().setStr_titlePrimaryText("你好 "+ LoginUtil.getUserBaseinfo().getUserName()+ " ");
            titleText.getValue().setStr_titleSecondaryText("智慧生活，生活更美好");
            setTitleTextMutableLiveData(titleText);
            textwIsChange.setValue(!textwIsChange.getValue());
            leftHeaderButton = 2;
            return;
        }
        if (type == 1){
            MutableLiveData<TitleText> titleText = getTitleTextMutableLiveData();
            titleText.getValue().setStr_titlePrimaryText("请绑定房屋 ");
            titleText.getValue().setStr_titleSecondaryText("绑定后享受更多服务 ");
            setTitleTextMutableLiveData(titleText);
            textwIsChange.setValue(!textwIsChange.getValue());
            leftHeaderButton = 1;
            return;
        }
    }

    /**
     * 设置没登录为未登录状态的titleText
     */
    public void noLoginTitle(){
        titleTextMutableLiveData.getValue().setStr_titlePrimaryText("您好，请登录 ");
        titleTextMutableLiveData.getValue().setStr_titleSecondaryText("登录后开启智慧生活");
        setTitleTextMutableLiveData(titleTextMutableLiveData);
    }

}
